package com.coolgor.coldot.web.auth;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.coolgor.coldot.dto.auth.LoginInfoExecution;
import com.coolgor.coldot.entity.auth.LoginInfo;
import com.coolgor.coldot.service.auth.LoginInfoService;


@Controller
@RequestMapping(value="auth", method={RequestMethod.GET,RequestMethod.POST})
public class LoginInfoController {
	@Autowired
	private LoginInfoService loginInfoService;
	
	@RequestMapping(value = "/getLoginInfoList", method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	private Map<String, Object> getLoginInfoList(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
	    int page=Integer.parseInt(request.getParameter("page"));
	    int pageSize=Integer.parseInt(request.getParameter("rows"));
	    LoginInfo loginInfoCondition = new LoginInfo();
	    String usernameCondition=request.getParameter("username");
	    if (!"".equals(usernameCondition)||null!=usernameCondition) {
	    	loginInfoCondition.setUsername(usernameCondition);
	    }
	    String usernameCNCondition=request.getParameter("usernameCN");	    
	    if (!"".equals(usernameCNCondition)||null!=usernameCNCondition) {
	    	loginInfoCondition.setNameCN(usernameCNCondition);
	    }
	    LoginInfoExecution exe = loginInfoService.getLoginInfoList(loginInfoCondition, page, pageSize);
		if (null != exe ) {
			try {
				modelMap.put("total",exe.getCount());
				modelMap.put("rows",exe.getLoginInfoList());
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "Empty List");
		}
		return modelMap;
	}	
	
}
